<template>
  <div>
    <div id="landingPageEcharts" style="width: 100%; height: 230px"></div>
  </div>
</template>

<script setup lang="ts">
import * as echarts from 'echarts'
import { onMounted } from 'vue'

const colors = ['#1cd389', '#668eff', '#ff6e73']

const legendData = [
  { title: '访问90%', value: '32人', percentage: '占比 34.56%' },
  { title: '访问80%', value: '--', percentage: '占比 14.39%' },
  { title: '访问30%', value: '56人', percentage: '占比 41.21%' }
]

const chartData = [
  { value: 34.56, name: '访问90%' },
  { value: 14.39, name: '访问80%' },
  { value: 41.21, name: '访问30%' }
]

onMounted(() => {
  const myChart = echarts.init(document.getElementById('landingPageEcharts')!)

  let chartData = [
    { value: 1048, name: 'Search Engine' },
    { value: 735, name: 'Direct' },
    { value: 580, name: 'Email' },
    { value: 484, name: 'Union Ads' },
    { value: 300, name: 'Video Ads' }
  ]
  var option

  option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      top: '0%',
      x: 'left',
      left: '60%',
      orient: 'vertical',
      data: chartData.map((item) => {
        return item.name
      })
    },
    series: [
      {
        name: 'Access From',
        type: 'pie',
        radius: ['60%', '80%'],
        center: ['25%', '50%'], // 调整中心点位置
        avoidLabelOverlap: false,
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 16,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: chartData
      }
    ]
  }
  option && myChart.setOption(option)
})
</script>

<style scoped>
.chart-container {
  position: relative;
  width: 100%;
  height: 200px;
  display: flex;
}

.custom-legend {
  flex: 1;
  padding-left: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.legend-item {
  margin-bottom: 15px;
  line-height: 1.5;
}

.dot {
  margin-right: 8px;
  font-size: 16px;
}

.title {
  font-weight: bold;
  margin-right: 10px;
}

.value {
  display: block;
  margin-left: 24px;
  color: #666;
}

.percentage {
  display: block;
  margin-left: 24px;
  color: #999;
  font-size: 12px;
}
</style>
